package com.advich.ai;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

@Service
public class RagService {
    private final VectorStore vectorStore;
    private final ChatClient chatClient;

    public RagService(VectorStore vectorStore, ChatClient chatClient) {
        this.vectorStore = vectorStore;
        this.chatClient = chatClient;
    }

    public String ask(String question) {
        var docs = vectorStore.similaritySearch(question, 2);
        String context = docs.stream().map(d -> d.getContent()).reduce("", (a, b) -> a + "\n" + b);
        String prompt = "已知信息如下：\n" + context + "\n\n请根据上述内容回答问题：" + question;
        return chatClient.call(prompt).getResult().getOutput().getContent();
    }
} 